package com.zb.set.controller;

import com.zb.annotation.SystemLog;
import com.zb.constant.OperationLogModuleType;
import com.zb.constant.OperationLogType;
import com.zb.entity.AccounLoginInfo;
import com.zb.exception.SMSException;
import com.zb.exception.ZbException;
import com.zb.result.PageUtil;
import com.zb.result.Result;
import com.zb.service.*;
import com.zb.set.service.ConSetService;
import com.zb.utils.TokenUtil;
import com.zb.vo.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.List;


/**
 * @Author WEN
 * @Date 2023/7/26 0026
 **/
@Api(tags = "系统设置类接口")
@RestController
@RequestMapping("/set")
public class SetController {
    @Resource
    private ConSetService conSetService;
    @Resource
    private ZjCodeRegSetService codeRegSetService;
    @Resource
    private ZjCodeSetService codeSetService;
    @Resource
    private ZjCodeScanDetailSetService codeScanDetailSetService;
    @Resource
    private ZjNetworkSetService networkSetService;
    @Resource
    private ZjNetworkInviteCodeSetService networkInviteCodeSetService;
    @Resource
    private ZjRepairSetService repairSetService;
    @Resource
    private ZjStoreSetService storeSetService;
    @Resource
    private ZjSysSetOverallService sysSetOverallService;
    @Resource
    private ZjUserSetService userSetService;
    @Resource
    private ZjWorkSetService workSetService;
    @Resource
    private ZjFactorySetService factorySetService;
    @Resource
    private ZjNetworkInviteCodeSendLogService networkInviteCodeSendLogService;
    @Resource
    private ZjWithdrawSetService withdrawSetService;

    @ApiOperation("获取系统全局设置")
    @GetMapping("/getSysSetOverall")
    public Result getSysSetOverall(HttpServletRequest request) {
        Long custAccId = TokenUtil.getcustAccIdByToken(request);
        return Result.success("获取系统全局设置数据成功").data("row", sysSetOverallService.getSysSetOverall(custAccId));
    }

    @ApiOperation("获取PC端Logo")
    @GetMapping("/getPcLog")
    public Result getPcLog(HttpServletRequest request) {
        Long custAccId = TokenUtil.getcustAccIdByToken(request);
        return Result.success("获取PC端Logo成功").data(PageUtil.row(sysSetOverallService.getPcLog(custAccId)));
    }

    @ApiOperation("获取系统全局设置的地址库级别")
    @GetMapping("/getAddressLevel")
    public Result getAddressLevel(HttpServletRequest request) {
//        Long custAccId = TokenUtil.getcustAccIdByToken(request);
        return Result.success("获取系统全局地址库级别成功").data("row", 2);
//        return Result.success("获取系统全局地址库级别成功").data("row", sysSetOverallService.getAddressLevel(custAccId));
    }

    @SystemLog(moduleType = OperationLogModuleType.SYSTEM_SET_TYPE, type = OperationLogType.UPDATE, remark = "系统设置-系统全局设置 - 更新系统全局设置")
    @ApiOperation("更新系统全局设置")
    @PutMapping("/saveSysSetOverall")
    public Result saveSysSetOverall(@RequestBody @Valid SysSetOverallVo overallVo) {
        sysSetOverallService.saveSysSetOverall(overallVo);
        return Result.success("更新系统全局设置成功");
    }

    @ApiOperation("获取编码设置")
    @GetMapping("/getCodeSet")
    public Result getCodeSet(HttpServletRequest request) {
        Long custAccId = TokenUtil.getcustAccIdByToken(request);
        return Result.success("获取编码设置数据成功").data("row", codeSetService.getCodeSet(custAccId));
    }

    @SystemLog(moduleType = OperationLogModuleType.SYSTEM_SET_TYPE, type = OperationLogType.UPDATE, remark = "系统设置-编码设置 - 保存编码设置")
    @ApiOperation("保存编码设置")
    @PutMapping("/saveCodeSet")
    public Result saveCodeSet(@RequestBody @Valid CodeSetVo codeSetVo) {
        codeSetService.saveCodeSet(codeSetVo);
        return Result.success("更新编码设置成功！");
    }

    @ApiOperation("获取编码扫码设置信息")
    @GetMapping("/getCodeScanSet")
    public Result getCodeScanSet(HttpServletRequest request) {
        Long custAccId = TokenUtil.getcustAccIdByToken(request);
        return Result.success().data(PageUtil.row(codeScanDetailSetService.getDetail(custAccId)));
    }

    @SystemLog(moduleType = OperationLogModuleType.SYSTEM_SET_TYPE, type = OperationLogType.UPDATE, remark = "系统设置-编码扫码信息设置 - 保存编码扫码信息设置")
    @ApiOperation("保存编码扫码信息设置")
    @PutMapping("/saveCodeScanSet")
    public Result saveCodeScanSet(HttpServletRequest request, @RequestBody @Valid List<CodeScanDetailSetVo>  scanDetailSetVos) {
        Long custAccId = TokenUtil.getcustAccIdByToken(request);
        codeScanDetailSetService.save(custAccId, scanDetailSetVos);
        return Result.success("更新编码扫码信息成功");
    }

    @ApiOperation("获取编码登记设置")
    @GetMapping("/getCodeRegSet")
    public Result getCodeRegSet(HttpServletRequest request) {
        Long custAccId = TokenUtil.getcustAccIdByToken(request);
        return Result.success("获取编码登记设置数据成功").data("row", codeRegSetService.getCodeRegSet(custAccId));
    }

    @SystemLog(moduleType = OperationLogModuleType.SYSTEM_SET_TYPE, type = OperationLogType.UPDATE, remark = "系统设置-编码登记设置 - 保存编码登记设置")
    @ApiOperation("保存编码登记设置")
    @PutMapping("/saveCodeRegSet")
    public Result saveCodeRegSet(@RequestBody @Valid CodeRegSetVo regSetVo) {
        codeRegSetService.saveCodeRegSet(regSetVo);
        return Result.success("更新编码登记设置成功！");
    }

    @ApiOperation("获取服务区域设置")
    @GetMapping("/getNetworkSet")
    public Result getNetworkSet(HttpServletRequest request) {
        Long custAccId = TokenUtil.getcustAccIdByToken(request);
        return Result.success("获取服务区域设置数据成功").data("row", networkSetService.getNetworkSet(custAccId));
    }

    @SystemLog(moduleType = OperationLogModuleType.SYSTEM_SET_TYPE, type = OperationLogType.UPDATE, remark = "系统设置-服务区域设置 - 保存服务区域设置")
    @ApiOperation("保存服务区域设置")
    @PutMapping("/saveNetworkSet")
    public Result saveNetworkSet(@RequestBody @Valid NetworkSetVo networkSetVo) {
        networkSetService.saveNetworkSet(networkSetVo);
        return Result.success("更新服务区域设置成功！");
    }

    @ApiOperation("获取服务商邀请码设置")
    @GetMapping("/getNetworkInviteCodeSet")
    public Result getNetworkInviteCodeSet(HttpServletRequest request) {
        Long custAccId = TokenUtil.getcustAccIdByToken(request);
        return Result.success("获取服务商邀请码设置成功").data(PageUtil.row(networkInviteCodeSetService.getNetworkInviteCodeSet(custAccId)));
    }

    @ApiOperation("保存服务商邀请码设置")
    @PostMapping("/saveNetworkInviteCode")
    public Result saveNetworkInviteCode(HttpServletRequest request, @RequestBody @Valid NetworkInviteCodeSetVo inviteCodeSetVo) {
        Long custAccId = TokenUtil.getcustAccIdByToken(request);
        networkInviteCodeSetService.saveNetworkInviteCode(custAccId, inviteCodeSetVo);
        return Result.success("更新成功");
    }

    @ApiOperation("生成/重新生成邀请码")
    @GetMapping("/generateInviteCode")
    public Result generateInviteCode(HttpServletRequest request) {
//        Long custAccId = TokenUtil.getcustAccIdByToken(request);
        AccounLoginInfo accountLoginInfo = TokenUtil.getAccountLoginInfo(request);
        return Result.success("邀请码生成成功").data(PageUtil.row(conSetService.generateInviteCode(accountLoginInfo)));
    }

    @ApiOperation("发送邀请码")
    @PostMapping("/sendSmsInviteCode")
    public Result sendSmsInviteCode(HttpServletRequest request, @RequestBody @ApiParam(value = "接收短信的手机号", required = true) List<String> phones) throws SMSException, ZbException {
        AccounLoginInfo loginInfo = TokenUtil.getAccountLoginInfo(request);
        conSetService.sendSmsInviteCode(loginInfo, phones);
        return Result.success("邀请码发送成功");
    }

    @ApiOperation("获取发送邀请码记录列表")
    @PostMapping("/getNetworkInviteCodeList")
    public Result getNetworkInviteCodeList(HttpServletRequest request, @RequestBody PageUtil pageUtil) {
        Long custAccId = TokenUtil.getcustAccIdByToken(request);
        return Result.success("获取发送邀请码记录列表成功").data(networkInviteCodeSendLogService.getNetworkInviteCodeList(custAccId, pageUtil));
    }

    @ApiOperation("获取返厂设置")
    @GetMapping("/getRepairSet")
    public Result getRepairSet(HttpServletRequest request) {
        Long custAccId = TokenUtil.getcustAccIdByToken(request);
        return Result.success("获取返厂设置数据成功").data("row", repairSetService.getRepairSet(custAccId));
    }

    @SystemLog(moduleType = OperationLogModuleType.SYSTEM_SET_TYPE, type = OperationLogType.UPDATE, remark = "系统设置-返厂设置 - 保存返厂设置")
    @ApiOperation("保存返厂设置")
    @PutMapping("/saveRepairSet")
    public Result saveRepairSet(@RequestBody @Valid RepairSetVo repairSetVo) {
        repairSetService.saveRepairSet(repairSetVo);
        return Result.success("更新返厂设置成功！");
    }

    @ApiOperation("获取仓库设置")
    @GetMapping("/getStorerSet")
    public Result getStorerSet(HttpServletRequest request) {
        Long custAccId = TokenUtil.getcustAccIdByToken(request);
        return Result.success("获取仓库设置数据成功").data("row", storeSetService.getStorerSet(custAccId));
    }

    @SystemLog(moduleType = OperationLogModuleType.SYSTEM_SET_TYPE, type = OperationLogType.UPDATE, remark = "系统设置-仓库设置 - 保存仓库设置")
    @ApiOperation("保存仓库设置")
    @PutMapping("/saveStoreSet")
    public Result saveStoreSet(@RequestBody @Valid StoreSetVo storeSetVo) {
        storeSetService.saveStoreSet(storeSetVo);
        return Result.success("更新仓库设置成功！");
    }

    @ApiOperation("获取用户设置")
    @GetMapping("/getUserSet")
    public Result getUserSet(HttpServletRequest request) {
        Long custAccId = TokenUtil.getcustAccIdByToken(request);
        return Result.success("获取用户设置数据成功").data("row", userSetService.getUserSet(custAccId));
    }

    @SystemLog(moduleType = OperationLogModuleType.SYSTEM_SET_TYPE, type = OperationLogType.UPDATE, remark = "系统设置-用户设置 - 保存用户设置")
    @ApiOperation("保存用户设置")
    @PutMapping("/saveUserSet")
    public Result saveUserSet(@RequestBody @Valid UserSetVo userSetVo) {
        userSetService.saveUserSet(userSetVo);
        return Result.success("更新用户设置成功！");
    }

    @ApiOperation("获取工单设置")
    @GetMapping("/getWorkSet")
    public Result getWorkSet(HttpServletRequest request) {
        Long custAccId = TokenUtil.getcustAccIdByToken(request);
        return Result.success("获取工单设置数据成功").data("row", workSetService.getWorkSet(custAccId));
    }

    @SystemLog(moduleType = OperationLogModuleType.SYSTEM_SET_TYPE, type = OperationLogType.UPDATE, remark = "系统设置-工单设置 - 保存工单设置")
    @ApiOperation("保存工单设置")
    @PutMapping("/saveWorkSet")
    public Result saveWorkSet(@RequestBody @Valid WorkSetVo workSetVo) {
        workSetService.saveWorkSet(workSetVo);
        return Result.success("更新工单设置成功！");
    }

    @ApiOperation("获取品牌厂商全局设置")
    @GetMapping("/getFactorySet")
    public Result getFactorySet(HttpServletRequest request) {
        Long custAccId = TokenUtil.getcustAccIdByToken(request);
        return Result.success("获取品牌厂商全局设置成功").data(PageUtil.row(factorySetService.getFactorySet(custAccId)));
    }

    @SystemLog(moduleType = OperationLogModuleType.SYSTEM_SET_TYPE, type = OperationLogType.UPDATE, remark = "系统设置-厂商设置 - 保存厂商设置")
    @ApiOperation("保存厂商设置")
    @PutMapping("/saveFactorySet")
    public Result saveFactorySet(HttpServletRequest request, @RequestBody @Valid FactorySetVo factorySetVo) {
        Long custAccId = TokenUtil.getcustAccIdByToken(request);
        factorySetService.saveFactorySet(custAccId, factorySetVo);
        return Result.success("保存厂商设置");
    }

    @ApiOperation("获取提现设置")
    @GetMapping("/getWithdrawSet")
    public Result getWithdrawSet(HttpServletRequest request) {
        Long custAccId = TokenUtil.getcustAccIdByToken(request);
        return Result.success("获取提现设置数据成功").data(PageUtil.row(withdrawSetService.getWithdrawSet(custAccId)));
    }

    @SystemLog(moduleType = OperationLogModuleType.SYSTEM_SET_TYPE, type = OperationLogType.UPDATE, remark = "系统设置-厂商设置 - 保存提现设置")
    @ApiOperation("保存提现设置")
    @PutMapping("/saveWithdrawSet")
    public Result saveWithdrawSet(HttpServletRequest request, @RequestBody @Valid WithdrawSetVo withdrawSetVo) {
        Long custAccId = TokenUtil.getcustAccIdByToken(request);
        withdrawSetService.save(custAccId, withdrawSetVo);
        return Result.success("更改提现设置成功");
    }










}
